package com.xuecheng.content.service.impl;

import com.alibaba.fastjson.JSON;
import com.xuecheng.base.exception.StudySystemException;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.mapper.CourseMarketMapper;
import com.xuecheng.content.mapper.CoursePublishMapper;
import com.xuecheng.content.mapper.CoursePublishPreMapper;
import com.xuecheng.content.model.dto.CourseBaseInfoDto;
import com.xuecheng.content.model.dto.CoursePreviewDto;
import com.xuecheng.content.model.dto.TeachplanDto;
import com.xuecheng.content.model.po.CourseBase;
import com.xuecheng.content.model.po.CourseMarket;
import com.xuecheng.content.model.po.CoursePublish;
import com.xuecheng.content.model.po.CoursePublishPre;
import com.xuecheng.content.service.CourseBaseInfoService;
import com.xuecheng.content.service.CoursePublishService;
import com.xuecheng.content.service.TeachplanService;
import com.xuecheng.messagesdk.service.MqMessageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @author Tang
 * @version 1.0
 * @date 2024/1/6 12:23
 */
@Slf4j
@Service
public class CoursePublishServiceImpl implements CoursePublishService {
    /** 课程已提交状态 **/
    private final String SUBMITTED = "202003";
    private final String APPROVED = "202004";

    private final CourseBaseInfoService courseBaseInfoService;
    private final TeachplanService teachplanService;
    private final CourseMarketMapper courseMarketMapper;
    private final CoursePublishPreMapper coursePublishPreMapper;
    private final CourseBaseMapper courseBaseMapper;
    private final CoursePublishMapper coursePublishMapper;
    private final MqMessageService mqMessageService;
    public CoursePublishServiceImpl(CourseBaseInfoService courseBaseInfoService,
                                    TeachplanService teachplanService,
                                    CourseMarketMapper courseMarketMapper,
                                    CoursePublishPreMapper coursePublishPreMapper,
                                    CourseBaseMapper courseBaseMapper,
                                    CoursePublishMapper coursePublishMapper,
                                    MqMessageService mqMessageService) {
        this.courseBaseInfoService = courseBaseInfoService;
        this.teachplanService = teachplanService;
        this.courseMarketMapper = courseMarketMapper;
        this.coursePublishPreMapper = coursePublishPreMapper;
        this.courseBaseMapper = courseBaseMapper;
        this.coursePublishMapper = coursePublishMapper;
        this.mqMessageService = mqMessageService;
    }

    @Override
    public CoursePreviewDto getCoursePreviewInfo(Long courseId) {
        CourseBaseInfoDto courseBaseInfoDto = courseBaseInfoService.selectCourseBaseInfoById(courseId);
        List<TeachplanDto> teachplanDtos = teachplanService.selectTeachplanNodes(courseId);
        CoursePreviewDto coursePreviewDto = new CoursePreviewDto();
        coursePreviewDto.setTeachplans(teachplanDtos);
        coursePreviewDto.setCourseBase(courseBaseInfoDto);
        return coursePreviewDto;
    }

    @Override
    @Transactional
    public void commitAudit(Long companyId, Long courseId) {
        CourseBaseInfoDto courseBaseInfoDto = courseBaseInfoService.selectCourseBaseInfoById(courseId);
        if (Objects.isNull(courseBaseInfoDto)) {
            StudySystemException.cast("课程不存在");
        }
        if (!companyId.equals(courseBaseInfoDto.getCompanyId())) {
            StudySystemException.cast("本机构只能提交本机构的课程");
        }
        String auditStatus = courseBaseInfoDto.getAuditStatus();
        if (SUBMITTED.equals(auditStatus)) {
            StudySystemException.cast("课程已提交");
        }
        if (StringUtils.isEmpty(courseBaseInfoDto.getPic())) {
            StudySystemException.cast("请上传课程图片");
        }
        // 查询课程计划
        List<TeachplanDto> teachplanDtos = teachplanService.selectTeachplanNodes(courseId);
        if (CollectionUtils.isEmpty(teachplanDtos)) {
            StudySystemException.cast("课程计划为空");
        }
        CoursePublishPre coursePublishPre = new CoursePublishPre();
        BeanUtils.copyProperties(courseBaseInfoDto, coursePublishPre);
        courseBaseInfoDto = null;
        coursePublishPre.setCompanyId(companyId);
        // 营销信息
        CourseMarket courseMarket = courseMarketMapper.selectById(courseId);
        coursePublishPre.setMarket(JSON.toJSONString(courseMarket));
        coursePublishPre.setTeachplan(JSON.toJSONString(teachplanDtos));
        // 状态已提交
        coursePublishPre.setStatus(SUBMITTED);
        // 提交时间
        coursePublishPre.setCreateDate(LocalDateTime.now());
        CoursePublishPre coursePublishPreObj = coursePublishPreMapper.selectById(courseId);
        if (Objects.isNull(coursePublishPreObj)) {
            coursePublishPreMapper.insert(coursePublishPre);
        } else {
            coursePublishPreMapper.updateById(coursePublishPre);
        }
        CourseBase courseBase = courseBaseMapper.selectById(courseId);
        courseBase.setAuditStatus(SUBMITTED);
        courseBaseMapper.updateById(courseBase);
    }

    @Override
    @Transactional
    public void publish(Long companyId, Long courseId) {
        CoursePublishPre coursePublishPre = coursePublishPreMapper.selectById(courseId);
        if (Objects.isNull(coursePublishPre)) {
            StudySystemException.cast("课程没有审核记录，无法发布");
        }
        if (!companyId.equals(coursePublishPre.getCompanyId())) {
            StudySystemException.cast("不能处理与本机构无关的课程发布信息");
        }
        if (!APPROVED.equals(coursePublishPre.getStatus())) {
            StudySystemException.cast("课程没审核通过不允许发布");
        }
        // 向课程发布表写数据
        CoursePublish select = coursePublishMapper.selectById(courseId);
        CoursePublish coursePublish = new CoursePublish();
        BeanUtils.copyProperties(coursePublishPre, coursePublish);
        if (Objects.isNull(select)) {
            coursePublishMapper.insert(coursePublish);
        } else {
            coursePublishMapper.updateById(coursePublish);
        }
        // 向信息表写入数据
        mqMessageService.addMessage("course_publish",
                String.valueOf(courseId),
                null,
                null);
        coursePublishPreMapper.deleteById(courseId);
    }

    @Override
    public CoursePublish getCoursePublish(Long courseId) {
        return coursePublishMapper.selectById(courseId);
    }

    @Override
    public CoursePreviewDto getPublish(Long courseId) {
        CoursePublish coursePublish = getCoursePublish(courseId);
        if (Objects.isNull(coursePublish)) {
            return null;
        }
        CoursePreviewDto coursePreviewDto = new CoursePreviewDto();
        CourseBaseInfoDto courseBaseInfoDto = new CourseBaseInfoDto();
        BeanUtils.copyProperties(coursePublish, courseBaseInfoDto);
        coursePreviewDto.setCourseBase(courseBaseInfoDto);
        List<TeachplanDto> teachplanDtos = JSON.parseArray(coursePublish.getTeachplan(), TeachplanDto.class);
        coursePreviewDto.setTeachplans(teachplanDtos);
        return coursePreviewDto;
    }
}
